package com.example.demo.entity;

import lombok.Data;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Data
public class TimeIntervalModel {
    private String startTime;
    private String endTime;

    public static boolean verifyRepeatTime(List<TimeIntervalModel> timeIntervals) throws Exception {
        boolean rs=true;
        List<Pub<Long, Long, TimeIntervalModel>> intervals = new ArrayList<>();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //开始时间必须小于结束时间
        for (TimeIntervalModel data : timeIntervals) {
            Long start=sdf.parse(data.getStartTime()).getTime();
            Long end = sdf.parse(data.getEndTime()).getTime();
            Pub pub = new Pub(start, end, data);
            intervals.add(pub);
        }
        //验证时间段是否重合
        Iterator<Pub<Long, Long, TimeIntervalModel>> iterator = intervals.iterator();
        while (iterator.hasNext()) {
            Pub<Long, Long, TimeIntervalModel> data = iterator.next();
            iterator.remove();
            for (Pub<Long, Long, TimeIntervalModel> compareData : intervals) {
                boolean is = data.getF2() < compareData.getF1() || compareData.getF2() < data.getF1();
                if (!is) {
                  rs=false;
                }
            }
        }
        return rs;
    }
}